// src/utils/request.js

import axios from 'axios'

// 创建 axios 实例
const axiosInstance = axios.create({
  // 基础配置
  baseURL: 'http://localhost:8121/api', // 从环境变量读取
  timeout: 60000, // 超时时间
  headers: {
    'Content-Type': 'application/json;charset=UTF-8'
  },
  withCredentials: true // 跨域携带 Cookie
})
// 响应拦截器
axiosInstance.interceptors.response.use(
    response => {
      if (response.data.code !== 0) {
        return Promise.reject(response.data)
      }
      return response.data.data
    },
    error => {
      if (error.response?.status === 401) {
        router.push('/user')
      }
      console.log(error)
      return Promise.reject(error)
    }
  )
// 导出实例
export default axiosInstance